package com.keyroy.game.puzzle_rpg.dialogs;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Spinner;
import org.eclipse.swt.widgets.Text;

import com.keyroy.game.puzzle_rpg.models.attribute.Param;
import com.keyroy.game.puzzle_rpg.models.role.Role;
import com.keyroy.game.puzzle_rpg.utils.FileUtil;
import com.keyroy.game.puzzle_rpg.views.IconView;

public class RoleEditor extends Shell {
	private Text role_name;
	private Text role_icon;
	private Text role_info;
	private IconView canvas;
	private Spinner role_strength;
	private Spinner role_dex;
	private Spinner role_knowledge;
	private Spinner role_vitality;

	/**
	 * Launch the application.
	 * 
	 * @param args
	 */
	public static void main(String args[]) {
		try {
			Display display = Display.getDefault();
			RoleEditor shell = new RoleEditor(display, null);
			shell.open();
			shell.layout();
			while (!shell.isDisposed()) {
				if (!display.readAndDispatch()) {
					display.sleep();
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public RoleEditor(Display display, Role role, final RoleListener roleListener) {
		this(display, roleListener);
		role_name.setText(text(role.name));
		role_info.setText(text(role.info));
		role_icon.setText(text(role.icon));
		canvas.setImage(role.icon);

		role_strength.setSelection(toInt(role.strength));
		role_dex.setSelection(toInt(role.dexterity));
		role_knowledge.setSelection(toInt(role.knowledge));
		role_vitality.setSelection(toInt(role.vitality));
	}

	/**
	 * @wbp.parser.constructor
	 */
	public RoleEditor(Display display, final RoleListener roleListener) {
		super(display, SWT.DIALOG_TRIM);

		canvas = new IconView(this);
		canvas.setBounds(10, 10, 100, 100);

		Label lblNewLabel = new Label(this, SWT.NONE);
		lblNewLabel.setBounds(135, 13, 35, 17);
		lblNewLabel.setText("\u540D\u79F0 : ");

		role_name = new Text(this, SWT.BORDER);
		role_name.setBounds(176, 10, 258, 23);

		role_icon = new Text(this, SWT.BORDER);
		role_icon.setEditable(false);
		role_icon.setBounds(176, 40, 172, 23);

		Button btnNewButton = new Button(this, SWT.NONE);
		btnNewButton.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				String path = FileUtil.selectFile(getShell(), null, null, new String[] { "*.png", "*.gif" });
				if (path != null) {
					role_icon.setText(path);
				}
				canvas.setImage(path);
			}
		});
		btnNewButton.setBounds(354, 38, 80, 27);
		btnNewButton.setText("\u9009\u62E9\u6587\u4EF6");

		Label lblNewLabel_1 = new Label(this, SWT.NONE);
		lblNewLabel_1.setBounds(135, 43, 35, 17);
		lblNewLabel_1.setText("\u56FE\u6807 : ");

		role_info = new Text(this, SWT.BORDER | SWT.WRAP);
		role_info.setBounds(176, 69, 258, 23);

		Label lblNewLabel_2 = new Label(this, SWT.NONE);
		lblNewLabel_2.setBounds(135, 72, 35, 17);
		lblNewLabel_2.setText("\u7B80\u4ECB : ");

		Label lblNewLabel_3 = new Label(this, SWT.NONE);
		lblNewLabel_3.setBounds(135, 127, 35, 17);
		lblNewLabel_3.setText("\u529B\u91CF : ");

		role_strength = new Spinner(this, SWT.BORDER);
		role_strength.setMaximum(65535);
		role_strength.setSelection(10);
		role_strength.setBounds(176, 124, 258, 23);

		Label label = new Label(this, SWT.NONE);
		label.setBounds(135, 156, 35, 17);
		label.setText("\u654F\u6377 : ");

		role_dex = new Spinner(this, SWT.BORDER);
		role_dex.setMaximum(65535);
		role_dex.setSelection(10);
		role_dex.setBounds(176, 153, 258, 23);

		Label label_1 = new Label(this, SWT.NONE);
		label_1.setBounds(135, 185, 35, 17);
		label_1.setText("\u667A\u6167 : ");

		role_knowledge = new Spinner(this, SWT.BORDER);
		role_knowledge.setMaximum(65535);
		role_knowledge.setSelection(10);
		role_knowledge.setBounds(176, 182, 258, 23);

		Label label_2 = new Label(this, SWT.NONE);
		label_2.setBounds(135, 214, 35, 17);
		label_2.setText("\u4F53\u529B : ");

		role_vitality = new Spinner(this, SWT.BORDER);
		role_vitality.setMaximum(65535);
		role_vitality.setSelection(10);
		role_vitality.setBounds(176, 211, 258, 23);

		Button btnNewButton_1 = new Button(this, SWT.NONE);
		btnNewButton_1.setBounds(10, 252, 111, 27);
		btnNewButton_1.setText("\u9ED8\u8BA4\u88C5\u5907");

		Button btnNewButton_2 = new Button(this, SWT.NONE);
		btnNewButton_2.setBounds(176, 252, 80, 27);
		btnNewButton_2.setText("\u9ED8\u8BA4\u6280\u80FD");

		Button btnNewButton_3 = new Button(this, SWT.NONE);
		btnNewButton_3.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (roleListener != null) {
					roleListener.onCreate(create());
				}
				dispose();
			}
		});
		btnNewButton_3.setBounds(354, 252, 80, 27);
		btnNewButton_3.setText("\u5B8C\u6210");

		Label label_3 = new Label(this, SWT.SEPARATOR | SWT.VERTICAL);
		label_3.setBounds(127, 10, 2, 241);

		Label label_4 = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
		label_4.setBounds(127, 108, 307, 2);
		createContents();
	}

	private final Role create() {
		Role role = new Role();
		role.name = toText(role_name);
		role.info = toText(role_info);
		role.icon = toText(role_icon);
		role.strength.value = role_strength.getSelection();
		role.dexterity.value = role_dex.getSelection();
		role.knowledge.value = role_knowledge.getSelection();
		role.vitality.value = role_vitality.getSelection();
		return role;
	}

	private final String toText(Text text) {
		if (text.getText().length() > 0) {
			return text.getText();
		}
		return null;
	}

	private final int toInt(Param param) {
		if (param != null) {
			return param.value;
		}
		return 0;
	}

	private final String text(String text) {
		if (text == null) {
			return "";
		} else {
			return text;
		}
	}

	/**
	 * Create contents of the shell.
	 */
	protected void createContents() {
		setText("\u4EBA\u7269\u7F16\u8F91\u7A97\u53E3");
		setSize(450, 315);

	}

	@Override
	protected void checkSubclass() {
		// Disable the check that prevents subclassing of SWT components
	}

	public interface RoleListener {
		public void onCreate(Role role);
	}

	public Canvas getCanvas() {
		return canvas;
	}
}
